package com.echovin.esb.gateway.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.echovin.esb.gateway.dto.*;
import com.echovin.esb.gateway.model.*;
import com.echovin.esb.gateway.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GatewayServiceController {
    @Autowired
    GatewayRegisterService registerService;
    @Autowired
    GatewayServiceService serviceService;
    @Autowired
    GatewayIPLimitService ipLimitService;
    @Autowired
    GatewayParameterValidateService parameterValidateService;
    @Autowired
    GatewayErrorResponseService errorResponseService;
    @Autowired
    GatewayRouteService routeService;

    @PostMapping("/service/registergroup")
    public ResultBody registerServiceGroup(JSONObject jsonObject) {
        GatewayRegister register = JSON.toJavaObject(jsonObject, GatewayRegister.class);
        int result = registerService.insert(register);
        if (result > 0) {
            return ResultBody.ok().data("注册成功");
        } else {
            return ResultBody.failed();
        }
    }

    @PostMapping("/service/unregistergroup")
    public ResultBody unRegisterServiceGroup(String groupId) {
        if (StringUtils.isEmpty(groupId)) {
            return ResultBody.failed();
        }
        GatewayRegister register = new GatewayRegister();
        GatewayServerGroupDto serverGroupDto = new GatewayServerGroupDto();
        serverGroupDto.setId(groupId);
        register.setServerGroup(serverGroupDto);
        int result = registerService.delete(register);
        if (result > 0) {
            return ResultBody.ok().data("删除成功");
        } else {
            return ResultBody.failed();
        }
    }

    @PostMapping("/service/unregisterservice")
    public ResultBody unRegisterService(String serviceId) {
        if (StringUtils.isEmpty(serviceId)) {
            return ResultBody.failed();
        }
        GatewayServer server = new GatewayServer();
        server.setId(serviceId);
        int result = serviceService.delete(server);
        if (result > 0) {
            return ResultBody.ok().data("删除成功");
        } else {
            return ResultBody.failed();
        }
    }

    @PostMapping("/service/updateservice")
    public ResultBody updateService(JSONObject jsonObject) {
        GatewayServerDto serverDto = JSON.toJavaObject(jsonObject, GatewayServerDto.class);
        GatewayServer server = new GatewayServer();
        server.setId(serverDto.getId());
        server.setServiceName(serverDto.getServiceName());
        server.setRouteRule(serverDto.getRouteRule());
        server.setPath(serverDto.getPath());
        server.setProtocol(serverDto.getProtocol());
        server.setRemark(serverDto.getRemark());
        int result = serviceService.update(server);
        if (result > 0) {
            return ResultBody.ok().data("修改成功");
        } else {
            return ResultBody.failed();
        }
    }

    @PostMapping("/service/deleroute")
    public ResultBody deleteRoute(String serviceId) {
        if (StringUtils.isEmpty(serviceId)) {
            return ResultBody.failed();
        }
        GatewayServer server = new GatewayServer();
        server.setId(serviceId);
        int result = routeService.delete(server);
        if (result > 0) {
            return ResultBody.ok().data("删除成功");
        } else {
            return ResultBody.failed();
        }
    }

    @PostMapping("/service/updateroute")
    public ResultBody updateRoute(JSONObject jsonObject) {
        GatewayServerDto serverDto = JSON.toJavaObject(jsonObject, GatewayServerDto.class);
        GatewayServer server = new GatewayServer();
        server.setId(serverDto.getId());
        server.setRouteRule(serverDto.getRouteRule());
        int result = routeService.update(server);
        if (result > 0) {
            return ResultBody.ok().data("修改成功");
        } else {
            return ResultBody.failed();
        }
    }

    @PostMapping("/service/deliplimit")
    public ResultBody deleteIpLimit(String serviceId) {
        if (StringUtils.isEmpty(serviceId)) {
            return ResultBody.failed();
        }
        GatewayIPLimitApi ipLimitApi = new GatewayIPLimitApi();
        ipLimitApi.setServiceId(serviceId);
        int result = ipLimitService.delete(ipLimitApi);
        if (result > 0) {
            return ResultBody.ok().data("删除成功");
        } else {
            return ResultBody.failed();
        }
    }

    @PostMapping("/service/updateiplimit")
    public ResultBody updateIpLimit(JSONObject jsonObject) {
        GatewayIPLimitApiDto ipLimitApiDto = JSON.toJavaObject(jsonObject, GatewayIPLimitApiDto.class);
        GatewayIPLimitApi ipLimitApi = new GatewayIPLimitApi();
        ipLimitApi.setId(ipLimitApiDto.getId());
        ipLimitApi.setPolicyType(ipLimitApiDto.getPolicyType());
        ipLimitApi.setIpAddress(ipLimitApiDto.getIpAddress());
        int result = ipLimitService.update(ipLimitApi);
        if (result > 0) {
            return ResultBody.ok().data("修改成功");
        } else {
            return ResultBody.failed();
        }
    }

    @PostMapping("/service/delparameter")
    public ResultBody deleteParameter(String actionId) {
        if (StringUtils.isEmpty(actionId)) {
            return ResultBody.failed();
        }
        GatewayParameterSchema parameterSchema = new GatewayParameterSchema();
        parameterSchema.setActionId(actionId);
        int result = parameterValidateService.delete(parameterSchema);
        if (result > 0) {
            return ResultBody.ok().data("删除成功");
        } else {
            return ResultBody.failed();
        }
    }

    @PostMapping("/service/updateparameter")
    public ResultBody updateParameter(JSONObject jsonObject) {
        GatewayParameterSchemaDto parameterSchemaDto = JSON.toJavaObject(jsonObject, GatewayParameterSchemaDto.class);
        GatewayParameterSchema parameterSchema = new GatewayParameterSchema();
        parameterSchema.setId(parameterSchemaDto.getId());
        parameterSchema.setCheckType(parameterSchemaDto.getCheckType());
        parameterSchema.setSchema(parameterSchemaDto.getSchema());
        int result = parameterValidateService.update(parameterSchema);
        if (result > 0) {
            return ResultBody.ok().data("修改成功");
        } else {
            return ResultBody.failed();
        }
    }

    @PostMapping("/service/delerrresponse")
    public ResultBody deleteErrorResponse(String actionId) {
        if (StringUtils.isEmpty(actionId)) {
            return ResultBody.failed();
        }
        GatewayErrorResponse errorResponse = new GatewayErrorResponse();
        errorResponse.setActionId(actionId);
        int result = errorResponseService.delete(errorResponse);
        if (result > 0) {
            return ResultBody.ok().data("删除成功");
        } else {
            return ResultBody.failed();
        }
    }

    @PostMapping("/service/updateerrresponse")
    public ResultBody updateErrorResponse(JSONObject jsonObject) {
        GatewayErrorResponseDto errorResponseDto = JSON.toJavaObject(jsonObject, GatewayErrorResponseDto.class);
        GatewayErrorResponse errorResponse = new GatewayErrorResponse();
        errorResponse.setId(errorResponseDto.getId());
        errorResponse.setErrorCode(errorResponseDto.getErrorCode());
        errorResponse.setErrorResponse(errorResponseDto.getErrorResponse());
        int result = errorResponseService.update(errorResponse);
        if (result > 0) {
            return ResultBody.ok().data("修改成功");
        } else {
            return ResultBody.failed();
        }
    }

}
